#coding:utf-8
import Utills as utills
from Utills import GetLog
import dao as dao
import traceback
import json
#"10011"_info{username:"",password:"",userid:""}-------hset----store userinfo--persistent
#userid_pool(userid1,userid2,userid3)---------set----for useridcreator test---persistent
#(userid1:session,userid2:session)-string--for onlineuser find-and--timeout---temporary
#"10011"_friendlist(userid:username,userid:username)--hset--for friendlist find---persistent
#requsted version
version="1.0"
#PING_TIME设置 /s
PING_TIME=3000
#服务器缓存好友添加时间
ADDFRIEND_TIME=3000
#log配置
log=GetLog()
#super_user initial
redis_client=dao.redis_client_dao()
#functions
def REG_method(data,client,test):
   result={} 
   try:
       if(test.get("error")=="0"):
           Regedid_list=redis_client.GetAllFromset("userid_pool")
           userid=utills.UserIdCreater(6)
           while (userid in Regedid_list):
            #print("userid exists!")
               userid=utills.UserIdCreater(6)
           user_info_dic={"username":data.get("username"), "password":data.get("password"), "userid":userid}
           redis_client.MutiHset(userid+"_info",user_info_dic)
           redis_client.SingleAddToSet("userid_pool", userid)
           result={"error":"0", 
                       "type":"REG", 
                       "message":"success", 
                       "userid":userid, 
                       "username":data.get("username"), 
                       "version":version}
           log.info("REG_request1:"+str(data))
           log.info("REG_response1:"+str(result))
       else:
           result=test
           log.error("REG_request:"+str(data))
           log.error("REG_response:"+str(result)) 
   except:
      log.error("REG_SERIOUS_ERROR:"+traceback.print_exc())
      result={   "error":"1", 
                 "type":"REG", 
                 "username":data.get("username"), 
                 "message":"注册服务请求失败:"+traceback.print_exc(), 
                 "version":version}
      log.error("REG_request:"+str(data))
      log.error("REG_response:"+str(result))             
   finally:
       result=json.dumps(result)
       if(data.get("username") and data.get("password")[0]):
       		client.publish("/listener/"+data.get("username")+data.get("password")[0], result)
       		log.info("RA:"+"/listener/"+data.get("username")+data.get("password")[0])
#       client.publish("/listener",result)
#       log.info("REG_request:"+json.dumps(data))
#       log.info("REG_response:"+result)
def LOGIN_method(data,client,test):
   result={}
   try:
       if(test.get("error")=="0"):
           Regedid_list=redis_client.GetAllFromset("userid_pool")
           if(data.get("userid") not in Regedid_list ):
               result={    "error":"2", 
                           "type":"LOGIN", 
                           "message":"用户Id输入错误,或未注册",
                           "version":version}
               log.error("LOGIN_request:"+str(data))
               log.error("LOGIN_response:"+str(result))
           else:
               user_password=data.get("password")
               user_info=redis_client.MutiHget(data.get("userid")+"_info", ["username", "password"])
               
               if(user_info[1]!=user_password):
                   result={   "error":"3", 
                              "type":"LOGIN", 
                              "userid":data.get("userid"), 
                              "message":"用户ID或密码错误", 
                              "version":version
                              }
                   log.error("LOGIN_request:"+str(data))
                   log.error("LOGIN_response:"+str(result))            
               else:
                   client_state=redis_client.GetString(data.get("userid"))
                   if(client_state):
                       session=client_state
                   else:
                       session=utills.HashCodeCreator(8)
                   result={    "error":"0", 
                               "type":"LOGIN", 
                               "message":"success", 
                               "username":user_info[0], 
                               "session":session, 
                               "version":version}
    #               redis_client.SingleHset("user_online_pool",{data.get("userid"):session})
                   redis_client.SetString(data.get("userid"),session,PING_TIME)
                   log.info("LOGIN_request:"+str(data))
                   log.info("LOGIN_response:"+str(result))
       else:
            result=test
            log.error("REG_request:"+str(data))
            log.error("REG_response:"+str(result)) 
   except:
      log.error("LOGIN_SERIOUS_ERROR:"+traceback.print_exc())
      result={   "error":"1", 
                  "type":"LOGIN",
                 "message":"登录服务请求失败:服务器繁忙", 
                 "version":version}
      log.error("LOGIN_request:"+str(data))
      log.error("LOGIN_response:"+str(result))            
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("password")[0]):
       		client.publish("/listener/"+data.get("userid")+data.get("password")[0], result)
       		log.info("RA:"+"/listener/"+data.get("userid")+data.get("password")[0])
#       log.info("LOGIN_request:"+data)
#       log.info("LOGIN_response:"+result)
def LOGOUT_method(data,client,test): 
   result={}
   try:
       if(test.get("error")=="0"):
           client_pool=redis_client.GetString(data.get("userid"))
           if(not client_pool):
    #           redis_client.SingleHset("user_online_pool",{data.get("userid"):session})
               result={   "error":"1", 
                          "type":"LOGOUT", 
                          "message":"已经登出,或错误断开连接", 
                          "version":version}
           elif(data.get("session")!=redis_client.GetString(data.get("userid"))):
               result={    "error":"1",
                          "type":"REGETSESSION",
                          "message":"再尝试威胁其他用户安全的操作，IP将被永封！", 
                          "version":version
                        }   
               log.error("LOGOUT_request:"+str(data))
               log.error("LOGOUT_response:"+str(result))
           else:
    #           redis_client.SingleHsetDel("user_online_pool", data.get("userid"))
               redis_client.SetString(data.get("userid"),data.get("session"),1)
               result={   "error":"0", 
                           "type":"LOGOUT",
                          "message":"success", 
                          "version":version
                        }
               log.info("LOGOUT_request:"+str(data))
               log.info("LOGOUT_response:"+str(result))
       else:
          result=test
          log.error("REG_request:"+str(data))
          log.error("REG_response:"+str(result)) 
   except:
      log.error("LOGOUT_SERIOUS_ERROR:"+traceback.print_exc())
      result={   "error":"1", 
                 "type":"LOGOUT", 
                 "username":data.get("username"), 
                 "message":"登出请求服务器繁忙", 
                 "version":version}
      log.error("LOGOUT_request:"+str(data))
      log.error("LOGOUT_response:"+str(result))            
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
       		client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)
       		log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session"))
def PING_method(data,client,test): 
   result={}
   try:
       if(test.get("error")=="0"):
           client_state=redis_client.GetString(data.get("userid"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
           if(not client_state):
               result={   
                          "error":"1", 
                          "type":"PING", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("PING_request:"+(data))
               log.error("PING_response:"+str(result))             
           else:
               redis_client.SetString(data.get("userid"),data.get("session"),PING_TIME)
               userlist=[]
               for user in redis_client.GetAllFromset("userid_pool"):
                   if(redis_client.GetString(user)):
                       userlist.append(user)
    #           redis_client.SingleHsetDel("user_online_pool", data.get("userid"))
               result={   "error":"0", 
                          "type":"PING",
                          "message":"success",
                          "userlist":userlist,
                          "version":version
                        }
               log.info("PING_request:"+str(data))
               log.info("PING_response:"+str(result))          
       else:
           result=test
           log.error("REG_request:"+str(data))
           log.error("REG_response:"+str(result)) 
   except:
      log.error("PING_SERIOUS_ERROR:"+traceback.print_exc())
      result={   "error":"1", 
                 "type":"PING", 
                 "message":"PING请求失败,服务器繁忙", 
                 "version":version}
      log.error("PING_request:"+str(data))
      log.error("PING_response:"+str(result))            
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
       		client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)
       		log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session"))
def USERLIST_method(data,client,test): 
   result={}
   try:
       if(test.get("error")=="0"):
           client_state=redis_client.GetString(data.get("userid"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
           if(not client_state):
               result={   "error":"1", 
                          "type":"USERLIST", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("USERLIST_request:"+str(data))
               log.error("USERLIST_response:"+str(result))             
                          
           else:
              redis_client.SetString(data.get("userid"),data.get("session"),PING_TIME)
              userlist=[]
              for user in redis_client.GetAllFromset("userid_pool"):
                   if(redis_client.GetString(user)):
                       userlist.append(user)
              result={    "error":"0",
                          "type":"USERLIST",
                          "userlist":userlist,
                          "message":"success", 
                          "version":version
                        }
              log.info("USERLIST_request:"+str(data))
              log.info("USERLIST_response:"+str(result))             
       else:
           result=test
           log.error("REG_request:"+str(data))
           log.error("REG_response:"+str(result)) 
   except:
      log.error("USERLIST_SERIOUS_ERROR:"+traceback.print_exc()) 
      result={   "error":"1", 
                 "type":"USERLIST", 
                 "message":"USERLIST请求失败,服务器繁忙", 
                 "version":version}
      log.error("USERLIST_request:"+str(data))
      log.error("USERLIST_response:"+str(result))             
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
       		client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)
       		log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session"))  
def CHAT_method(data,client,test): 
   result={}
   try:
        if(test.get("error")=="0"):       
            client_state=redis_client.GetString(data.get("userid"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
            if(not client_state):
               result={   "error":"1", 
                          "type":"CHAT", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("CHAT_request:"+str(data))
               log.error("CHAT_response:"+str(result))             
            elif(data.get("session")!=redis_client.GetString(data.get("userid"))):
               result={    "error":"1",
                          "type":"REGETSESSION",
                          "message":"再尝试威胁其他用户安全的操作，IP将被永封！", 
                          "version":version
                        }
               log.error("CHAT_request:"+str(data))
               log.error("CHAT_response:"+str(result))
            else:
              redis_client.SetString(data.get("userid"),data.get("session"),PING_TIME)
              onlinelist={}
              offlinelist=[]
              for request_user in data.get("request"):
                  user_friendic=redis_client.HgetAll(data.get("userid")+"_friendlist")
                  if(user_friendic):
                      isfriend=user_friendic.get(request_user)
                      user_session=redis_client.GetString(request_user)
                  else:
                      isfriend=False
                      user_session=False
                  if(user_session and isfriend):
#                      user_dir={request_user:user_session}
                      onlinelist[request_user]=user_session
                      #通知被要求聊天用户session已被其他用户得知
                      result_to={
                         "error":"0",
                         "type":"CHATWARNING",
                         "message":"有用户请求聊天，请注意session安全",
                         "chat_request":[data.get("userid"),data.get("session")],
                         "version":version
                         }
                      result_to=json.dumps(result_to)
                      client.publish("/listener/"+request_user+"/"+user_session, result_to)
                 #找到离线且注册用户
                  else:
                      userid_pool=redis_client.GetAllFromset("userid_pool")
                      if(request_user in userid_pool):
                          offlinelist.append(request_user)
             
                
              #发回给请求方的消息
              result={    
                          "error":"0",
                          "type":"CHAT",
                          "online":onlinelist,
                          "offline":offlinelist,
                          "message":"success", 
                          "version":version
                        }
              log.info("CHAT_request:"+str(data))
              log.info("CHAT_response:"+str(result))   
        else:
            result=test
            log.error("REG_request:"+str(data))
            log.error("REG_response:"+str(result)) 
   except:
          log.error("CHAT_SERIOUS_ERROR:"+traceback.print_exc())
          result={
                 "error":"1", 
                 "type":"CHAT", 
                 "message":"CHAT请求失败,服务器繁忙", 
                 "version":version}
          log.error("CHAT_request:"+str(data))
          log.error("CHAT_response:"+str(result))         
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
       		client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)
       		log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session"))
def ADDFRIEND_method(data,client,test): 
   result={}
   try:
        isresult_to_ok=False
        isnotfriend=True
        if(test.get("error")=="0"):
            client_state=redis_client.GetString(data.get("userid"))
            client_re_state=redis_client.GetString(data.get("request"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
            if(not client_state):
               result={   "error":"1", 
                          "type":"ADDFRIEND", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("ADDFRIEND_request:"+str(data))
               log.error("ADDFRIEND_response:"+str(result))             
            elif(data.get("session")!=redis_client.GetString(data.get("userid"))):
               result={    "error":"1",
                          "type":"ADDFRIEND",
                          "message":"再尝试威胁其他用户安全的操作，IP将被永封！", 
                          "version":version
                        }
               log.error("ADDFRIEND_request:"+str(data))
               log.error("ADDFRIEND_response:"+str(result))            
            else:
              redis_client.SetString(data.get("userid"),data.get("session"),PING_TIME)
    #          username=redis_client.SingleHget(data.get("request")+"_info","username")
              userid_to=redis_client.SingleHget(data.get("request")+"_info","userid")
              userid_self=redis_client.SingleHget(data.get("userid")+"_info","userid")
              self_frienddict=redis_client.HgetAll(data.get("userid")+"_friendlist")
              if(self_frienddict):
                  self_friendlist=self_frienddict.keys()
              else:
                  self_friendlist=[]
              if((not userid_to)):
                  result={"error":"1",
                          "type":"ADDFRIEND",
                          "message":"请求添加的用户不存在",
                          "version":version
                          }
                  log.error("ADDFRIEND_request:"+str(data))
                  log.error("ADDFRIEND_response:"+str(result))           
              
              elif(userid_to==userid_self):
                  result={"error":"1",
                          "type":"ADDFRIEND",
                          "message":"能不调戏服务器嘛？你加自己好友是出于什么考虑?",
                          "version":version
                          }
                  log.error("ADDFRIEND_request:"+str(data))
                  log.error("ADDFRIEND_response:"+str(result))
              elif(not client_re_state):
                  result={"error":"1",
                          "type":"ADDFRIEND",
                          "message":"请求的用户不在线",
                          "version":version
                          }
                  log.error("ADDFRIEND_request:"+str(data))
                  log.error("ADDFRIEND_response:"+str(result))
              elif(data.get("request") in self_friendlist):
                  result={"error":"1",
                          "type":"ADDFRIEND",
                          "message":"请求的用户已在好友列表中",
                          "version":version
                          }
                  log.error("ADDFRIEND_request:"+str(data))
                  log.error("ADDFRIEND_response:"+str(result))
              else:
                    redis_client.SetString(data.get("userid")+"_add"+data.get("request"),data.get("request")+"_add"+data.get("userid"),ADDFRIEND_TIME)
                    userid_request=redis_client.GetString(data.get("request")+"_add"+data.get("userid"))
                    username_s=redis_client.SingleHget(data.get("userid")+"_info","username")
                    username_r=redis_client.SingleHget(data.get("request")+"_info","username")
                    if(userid_request and userid_request==data.get("userid")+"_add"+data.get("request")):
                        redis_client.SingleHset(data.get("userid")+"_friendlist",data.get("request"),username_r)
                        redis_client.SingleHset(data.get("request")+"_friendlist",data.get("userid"),username_s)
                        redis_client.SetString(data.get("userid")+"_add"+data.get("request"),"delete",1)
                        redis_client.SetString(data.get("request")+"_add"+data.get("userid"),"delete",1)
                        isnotfriend=False
                    user_re_dic=[data.get("userid"),username_s]
                    result={    
                          "error":"0",
                          "type":"ADDFRIEND", 
                          "message":"success", 
                          "version":version
                         }
                    result_for={
                           "error":"0",
                            "type":"ADDFRIEND_TO",
                            "message":"success",
                            "request_user":user_re_dic,
                            "version":version
                            
                           }
                    result_for=json.dumps(result_for)
                    isresult_to_ok=True
                    log.info("ADDFRIEND_request:"+str(data))
                    log.info("ADDFRIEND_response:"+str(result))
		 
        else:
             result=test
             log.error("REG_request:"+str(data))
             log.error("REG_response:"+str(result)) 
   except:
          log.error("ADDFRIEND_SERIOUS_ERROR:"+traceback.print_exc())
          result={
                 "error":"1", 
                 "type":"ADDFRIEND", 
                 "message":"ADDFRIEND请求失败,服务器繁忙", 
                 "version":version}
          log.error("ADDFRIEND_request:"+str(data))
          log.error("ADDFRIEND_response:"+str(result))          
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
                    client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)
                    if(data.get("request") and client_re_state and isresult_to_ok and isnotfriend):
#                        print("RA:"+"/listener/"+data.get("request")+"/"+client_re_state)
                        client.publish("/listener/"+data.get("request")+"/"+client_re_state,result_for)
#                        print(result_for)
#                        print("ok")
                        log.info("RA:"+"/listener/"+data.get("request")+"/"+client_re_state)
                    log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session"))
def DELFRIEND_method(data,client,test): 
   result={}
   try:
        if(test.get("error")=="0"):
            client_state=redis_client.GetString(data.get("userid"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
            if(not client_state):
               result={   "error":"1", 
                          "type":"DELFRIEND", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("DELFRIEND_request:"+str(data))
               log.error("DELFRIEND_response:"+str(result))              
            elif(data.get("session")!=redis_client.GetString(data.get("userid"))):
               result={    "error":"1",
                          "type":"DELFRIEND",
                          "message":"再尝试威胁其他用户安全的操作，IP将被永封！", 
                          "version":version
                        }
               log.error("DELFRIEND_request:"+str(data))
               log.error("DELFRIEND_response:"+str(result))            
            else:
              redis_client.SetString(data.get("userid"),data.get("session"),PING_TIME)
              username_to=redis_client.SingleHget(data.get("request")+"_info","username")
              username_self=redis_client.SingleHget(data.get("userid")+"_info","username")
              userid_to=redis_client.SingleHget(data.get("request")+"_info","userid")
              friend_exists=redis_client.Hexists(data.get("userid")+"_friendlist",userid_to)
              userid_self=redis_client.SingleHget(data.get("userid")+"_info","userid")
              if(not userid_to):
                  result={"error":"1",
                          "type":"DELFRIEND",
                          "message":"用户不存在",
                          "version":version
                          }
                  log.error("DELFRIEND_request:"+str(data))
                  log.error("DELFRIEND_response:"+str(result))           
              elif(userid_to==userid_self):
                  result={"error":"1",
                          "type":"ADDFRIEND",
                          "message":"能不调戏服务器嘛？你都不能加自己好友还要删除自己?",
                          "version":version
                          }
                  log.error("DELFRIEND_request:"+str(data))
                  log.error("DELFRIEND_response:"+str(result))          
              elif(not friend_exists):
                   result={"error":"1",
                          "type":"DELFRIEND",
                          "message":"少年，你们并不熟(好友列表查无此人)",
                          "version":version
                          }
                   log.error("DELFRIEND_request:"+str(data))
                   log.error("DELFRIEND_response:"+str(result))         
              else:
#                  username=redis_client.SingleHget(data.get("request")+"_info","username")
                  result={    
                          "error":"0",
                          "type":"DELFRIEND", 
                          "message":"success", 
                          "version":version
                        }
                  log.info("DELFRIEND_request:"+str(data))
                  log.info("DELFRIEND_response:"+str(result))        
                  del_friend=[userid_to,username_to]
                  del_friend_to=[data.get("userid"),username_self]
                  redis_client.MutiHsetDel(data.get("userid")+"_friendlist",del_friend)
                  print(del_friend_to)
                  redis_client.MutiHsetDel(userid_to+"_friendlist",del_friend_to)
        else:
             result=test
             log.error("REG_request:"+str(data))
             log.error("REG_response:"+str(result)) 
   except:
          log.error("DELFRIEND_SERIOUS_ERROR:"+traceback.print_exc())
          result={
                 "error":"1", 
                 "type":"DELFRIEND", 
                 "message":"DELFRIEND请求失败,服务器繁忙", 
                 "version":version}
          log.error("DELFRIEND_request:"+str(data))
          log.error("DELFRIEND_response:"+str(result))         
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
         client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)
         log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session")) 
def LISTFRIEND_method(data,client,test): 
   result={}
   try: 
        if(test.get("error")=="0"):
            client_state=redis_client.GetString(data.get("userid"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
            if(not client_state):
               result={   "error":"1", 
                          "type":"LISTFRIEND", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("LISTFRIEND_request:"+str(data))
               log.error("LISTFRIEND_response:"+str(result))              
            elif(data.get("session")!=redis_client.GetString(data.get("userid"))):
                result={    "error":"1",
                          "type":"REGETSESSION",
                          "message":"再尝试威胁其他用户安全的操作，IP将被永封！", 
                          "version":version
                        }
                log.error("LISTFRIEND_request:"+str(data))
                log.error("LISTFRIEND_response:"+str(result))         
            else:
              redis_client.SetString(data.get("userid"),data.get("session"),PING_TIME)
              frienddic=redis_client.HgetAll(data.get("userid")+"_friendlist")
              if(frienddic): 
                  result={    
                          "error":"0",
                          "type":"LISTFRIEND", 
                          "message":"success", 
                          "friendlist":frienddic,
                          "version":version
                        }
                  log.info("LISTFRIEND_request:"+str(data))
                  log.info("LISTFRIEND_response:"+str(result))      
              else:
                  result={    
                          "error":"0",
                          "type":"LISTFRIEND", 
                          "message":"用户好友列表not exist",
			  "friendlist":{}, 
                          "version":version
                        }
                  log.error("LISTFRIEND_request:"+str(data))
                  log.error("LISTFRIEND_response:"+str(result))
        else:
            result=test
            log.error("REG_request:"+str(data))
            log.error("REG_response:"+str(result)) 
                  
   except:
          log.error("LISTFRIEND_SERIOUS_ERROR:"+traceback.print_exc())       
          result={
                 "error":"1", 
                 "type":"DELFRIEND", 
                 "message":"DELFRIEND请求失败,服务器繁忙", 
                 "version":version}
          log.error("LISTFRIEND_request:"+str(data))
          log.error("LISTFRIEND_response:"+str(result))        
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
             client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result)      
             log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session")) 
def REGETSESSION_method(data,client,test):
   result={}
   try:
       if(test.get("error")=="0"):
           client_state=redis_client.GetString(data.get("userid"))
    #       client_pool=redis_client.Hexists("user_online_pool", data.get("userid"))
           if(not client_state):
               result={   "error":"1", 
                          "type":"REGETSESSION", 
                          "message":"已经登出,或错误断开连接,尝试重登录", 
                          "version":version}
               log.error("REGETSESSION_request:"+str(data))
               log.error("REGETSESSION_response:"+str(result))           
           elif(data.get("session")!=redis_client.GetString(data.get("userid"))):
               result={    "error":"1",
                          "type":"REGETSESSION",
                          "message":"再尝试威胁其他用户安全的操作，IP将被永封！", 
                          "version":version
                        }
                   
           else:
              new_session=utills.HashCodeCreator(8)
              redis_client.SetString(data.get("userid"),new_session,PING_TIME)
              result={    "error":"0",
                          "type":"REGETSESSION",
                          "newsession":new_session,
                          "message":"success", 
                          "version":version
                        }
              log.info("REGETSESSION_request:"+str(data))
              log.info("REGETSESSION_response:"+str(result))
       else:
          result=test
          log.error("REG_request:"+str(data))
          log.error("REG_response:"+str(result)) 
       
   except:
      log.error("REGETSESSION_SERIOUS_ERROR:"+traceback.print_exc())
      result={   
                 "error":"1", 
                 "type":"REGETSESSION", 
                 "message":"REGETSESSION请求失败,服务器繁忙", 
                 "version":version}
      log.error("REGETSESSION_request:"+str(data))
      log.error("REGETSESSION_response:"+str(result))            
   finally:
       result=json.dumps(result)
       if(data.get("userid") and data.get("session")):
       		client.publish("/listener/"+data.get("userid")+"/"+data.get("session"), result) 
       		log.info("RA:"+"/listener/"+data.get("userid")+"/"+data.get("session")) 
